<template>
  <view class="flex-col page">
    <view class="product-detail">
      <image class="product-image" src="../../static/images/51276e00a4890f11936fcc00b81e6367.png" />
      <text class="product-name">{{ name_commodity }}</text>
      <text class="product-description">{{ description_commodity }}</text>
      <text class="product-price">Price: ￥{{ money_commodity }}</text>
      <view class="flex-row">
		<div>  
			<button class="buy-button" @click="showBuyModal" >购买</button>
			 <button class="add-to-cart-button" @click="addToCart">加入购物车</button>
			 <!-- 购买弹窗 -->
			    <div v-if="showModal" class="modal">
			      <div class="modal-content">
			        <div class="product-info">
			          <p>要购买的商品数量：</p>
			          <div class="quantity">
			            <button @click="decreaseQuantity">-</button>
			            <span>{{ number_shopping }}</span>
			            <button @click="increaseQuantity">+</button>
			          </div>
			        </div>
			        <div class="buttons">
			          <button @click="confirmBuy">确定购买</button>
			          <button @click="cancelBuy">取消</button>
			        </div>
			      </div>
			    </div>
		</div>
        <!-- <button class="add-to-cart-button" @click="addToCart">加入购物车</button> -->
      </view>
    </view>
  </view>
</template>

<script>
	import { _usershop } from '@/service/shop.js'
	export default {
		data() {
			return {
				name_commodity:'',
				description_commodity:'',
				money_commodity:'',
				serialnum_commodity:'',
				number_shopping:'',
				account_user:'',
				showModal: false, // 是否显示购买弹窗
				number_shopping: 1 // 购买数量
			}
		},
		onShow(){
			try {
				const value = uni.getStorageSync('shopxiangqing');
				console.log(value)
				if (value != null) {
					this.name_commodity = value.name_commodity;
					this.description_commodity = value.description_commodity;
					this.money_commodity = value.money_commodity;
					this.serialnum_commodity = value.serialnum_commodity;
					console.log(this.value)
				}
			} catch (e) {
				console.error(e);
			}
		},
		methods: {
			 // 显示购买弹窗
			    showBuyModal() {
			      this.showModal = true;
			    },
			    // 隐藏购买弹窗
			    hideBuyModal() {
			      this.showModal = false;
			    },
			    // 减少购买数量
			    decreaseQuantity() {
			      if (this.number_shopping > 1) {
			        this.number_shopping;
			      }
			    },
			    // 增加购买数量
			    increaseQuantity() {
			      this.number_shopping++;
			    },
			    // 确定购买
			    confirmBuy() {
			      // 调用购买接口或执行其他购买操作
			      // ...
			
			      // 关闭购买弹窗
				// 显示购买成功提示
				  this.$api.msg('购买成功！');
			      this.hideBuyModal();
			    },
			    // 取消购买
			    cancelBuy() {
			      // 关闭购买弹窗
			      this.hideBuyModal();
			    },
			async buyProduct() {
			  //     let data={
			  //     	account_user:this.account_user,
					// serialnum_commodity:this.serialnum_commodity,
					// number_shopping:this.number_shopping
			  //     }
			  
			    },
			addToCart() {
			  // Call the add to cart API
			}
		},
		mounted() {
		  const shopxiangqingData = uni.getStorageSync('shopxiangqing');
		  if (shopxiangqingData) {
		    this.name_commodity = shopxiangqingData.data.name_commodity;
		    this.description_commodity = shopxiangqingData.data.description_commodity;
		    this.money_commodity = shopxiangqingData.data.money_commodity;
			this.serialnum_commodity = shopxiangqingData.data.serialnum_commodity;
		  }
		}
	}
</script>

<style scoped>
.product-detail {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
  height: 100vh;
}

.product-image {
  width: 200px;
  height: 200px;
  margin-bottom: 20px;
}

.product-name {
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 10px;
}

.product-description {
  margin-bottom: 10px;
}

.product-price {
  margin-bottom: 20px;
}

.buy-button, .add-to-cart-button {
  padding: 10px 20px;
  margin-right: 10px;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
}

.buy-button {
  background-color: green;
  color: white;
}

.add-to-cart-button {
  background-color: blue;
  color: white;
}
.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
}

.modal-content {
  width: 300px;
  background-color: #fff;
  padding: 20px;
  border-radius: 4px;
}

.product-info {
  margin-bottom: 20px;
}

.quantity {
  display: flex;
  align-items: center;
}

.quantity button {
  width: 30px;
  height: 30px;
  border: none;
  background-color: #eee;
  cursor: pointer;
}

.buttons {
  display: flex;
  justify-content: space-between;
}
</style>